const  app =  getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    progress: [1, 0, 0],   // 0默认  1制作中  2已完成
    img:'https://files.dianlinet.com/dianli/images/2020-06-12/17-04-09.jpg',   //传过来的logo
    imglist: [],
    log_id:'',
    type: ['单品方案', '礼包方案'],
    region:-1,
    typefont:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.func.onPageLoad(this, options);
    app.func.getPromise('/plan/info/' + options.log_id)
        .then(([code,res])=>{
          if(res.data.logo.length > 0){
              this.setData({ img:res.data.logo });
          }
          this.setData({
            oData:res.data,
            log_id: options.log_id
          });
        })
  },

  bindRegionChange: function (e) {
    let index = e.detail.value;
    this.setData({ region: index });
  },

  img_w_show: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(res);
        that.setData({
          imglist: res.tempFilePaths
        })

      }
    })
  },
  //图片点击事件
  imgYu: function (e) {
    let src = e.currentTarget.dataset.src;
    wx.previewImage({
      urls: [src]
    })
  },

  formsubmit: function (e) {
    console.log(e.detail.value.type)
    let type = this.data.log_id==0 ? e.detail.value.type : this.data.oData.type;
    let title = e.detail.value.title;
    let name = e.detail.value.name;
    let log_id = this.data.log_id;
    let tel = e.detail.value.tel;
    let content = e.detail.value.content;
    if (title.length == 0) {
      app.func.toastPromise('方案名称不能为空');
      return;
    }
    if (type == null) {
      app.func.toastPromise('请选择方案类型');
      return;
    }
    if (name.length == 0) {
      app.func.toastPromise('联系人不能为空');
      return;
    }
    if (tel.length == 0) {
      app.func.toastPromise('电话不能为空');
      return;
    }
    else {
      wx.showLoading({title: '提交中'});
      let uploadimg;
      let promise = app.func.resolve();
      promise = promise.then(() => {
        let imglist = this.data.imglist[0];
        let img = this.data.img;
        if(imglist){
              return app.func.uploadPromise("/image/upload", imglist, 'file',).then(function ([code, res]) {
                if(code == 3102 || code == 3103){
                  return app.func.toastPromise('图片内容违规').then((resolve, reject)=>{reject();});
                } else {
                  uploadimg = res.data;
                }
              });
        }else if(img.length > 0){
            uploadimg = img;
        }else{
          uploadimg = '';
        }
      });

      promise.then(() => {
        // this.modify();


        let that = this;
        app.func.postPromise('/plan/modify',
            {
              logo: uploadimg,
              title: title,
              name: name,
              tel: tel,
              content: content,
              log_id:log_id,
              type:type
            })
            .then(([code, res]) => {
              wx.hideLoading();
              let id = res.data;
              if (code == 200) {
                app.func.toastPromise('修改成功')
                    .then(()=>{
                       that.checkjump(id,type);
                    });
              } else {
                app.func.toastPromise(res.message);
              }
            })
      })
    }
  },


  checkjump: function(id,type) {
  console.log(type)
       let log_id = this.data.log_id;
       if(log_id == 0){
         wx.redirectTo({
           url:`/tools/pages/tool/recommending/recommending?log_id=${id}&type=${type}`
         })
       }else{
         wx.navigateBack({delta: 1,})
       }
  }

})
